package com.starlight.flower_server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.authentication.AuthenticationCheck;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.order.cqrs.CompleteOrderCommand;
import com.starlight.flower_server.domain.order.cqrs.OrderPageQuery;
import com.starlight.flower_server.domain.order.cqrs.PayOrderCommand;
import com.starlight.flower_server.domain.order.data.OrderData;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.entity.Order;
import com.starlight.flower_server.domain.service.OrderService;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/5 15:06
 * 订单接口
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("/getOrderDetailById")
    public FlowerServerResponse<Order> getOrderDetailById(Long id){
        return FlowerServerResponse.success(orderService.queryOrderDetailById(id));
    }

    @PostMapping("/deleteOrderById")
    @AuthenticationCheck(roleType = {RoleType.ROOT})
    public FlowerServerResponse<Integer> deleteOrderById(Long id){
        return FlowerServerResponse.success(orderService.logicDeleteOrderById(id));
    }

    @PostMapping("/createOrder")
    @AuthenticationCheck(roleType = {RoleType.ROOT,RoleType.NORMAL,RoleType.PRIMARY,RoleType.SENIOR})
    public FlowerServerResponse<Integer> createOrder(@RequestBody Order order){
        return FlowerServerResponse.success(orderService.insertOrder(order));
    }

    @PostMapping("/updateOrder")
    public FlowerServerResponse<Integer> updateOrder(@RequestBody Order order){
        return FlowerServerResponse.success(orderService.modifyOrder(order));
    }

    @GetMapping("/getOrderPage")
    public FlowerServerResponse<IPage<OrderData>> getOrderPage(OrderPageQuery query){
        return FlowerServerResponse.success(orderService.queryOrderList(query));
    }

    @GetMapping("/getFlowerUserOrderByFlowerUserId")
    public FlowerServerResponse<List<OrderData>> getFlowerUserOrderByFlowerUserId(Long id){
        return FlowerServerResponse.success(orderService.queryFlowerUserOrderByFlowerUserId(id));
    }

    @GetMapping("/getFlowerUserOrderByCurrentUser")
    @AuthenticationCheck(roleType = {RoleType.ROOT,RoleType.NORMAL,RoleType.PRIMARY,RoleType.SENIOR})
    public FlowerServerResponse<List<OrderData>> getFlowerUserOrderByCurrentUser(){
        Long id= Long.valueOf(CurrentUserInfoUtils.getUserId());
        return FlowerServerResponse.success(orderService.queryFlowerUserOrderByFlowerUserId(id));
    }

    @GetMapping("/getAllOrderData")
    public FlowerServerResponse<TotalData<OrderData>> getAllOrderData(){
        return FlowerServerResponse.success(orderService.queryAllOrderData());
    }

    @GetMapping("/getOrderTotalCount")
    public FlowerServerResponse<TotalData<OrderData>> getOrderTotalCount(){
        return FlowerServerResponse.success(orderService.queryOrderTotalCount());
    }

    @PostMapping("/payOrder")
    public FlowerServerResponse<Integer> payOrder(PayOrderCommand command){
        return FlowerServerResponse.success(orderService.paySelectedOrder(command.getOrderId()));
    }

    @PostMapping("/completeOrder")
    public FlowerServerResponse<Integer> completeOrder(CompleteOrderCommand command){
        return FlowerServerResponse.success(orderService.completeSelectedOrder(command.getOrderId()));
    }

}